home *** CD-ROM | disk | FTP | other *** search
- /*
- File: NavigationServicesSupport.c
-
- Copyright: © 1997-1998 by Apple Computer, Inc., all rights reserved.
-
- */
-
- #include "NavigationServicesSupport.h"
-
-
- #include <CodeFragments.h>
- #include <Finder.h>
- #include <Dialogs.h>
- #include <LowMem.h>
- #include <string.h>
- #include <Processes.h>
-
- extern void ConductErrorDialog(OSErr error, short commandID, short alertType);
-
- static Handle NewOpenHandle(OSType applicationSignature, short numTypes, OSType typeList[])
- {
- Handle hdl = NULL;
-
- if ( numTypes > 0 )
- {
-
- hdl = NewHandle(sizeof(NavTypeList) + numTypes * sizeof(OSType));
-
- if ( hdl != NULL )
- {
- NavTypeListHandle open = (NavTypeListHandle)hdl;
-
- (*open)->componentSignature = applicationSignature;
- (*open)->osTypeCount = numTypes;
- BlockMoveData(typeList, (*open)->osType, numTypes * sizeof(OSType));
- }
- }
-
- return hdl;
- }
-
-
-
-
- static OSStatus SendOpenAE(AEDescList list)
- {
- OSStatus err;
- AEAddressDesc theAddress;
- AppleEvent dummyReply;
- AppleEvent theEvent;
-
- theAddress.descriptorType = typeNull;
- theAddress.dataHandle = NULL;
-
- do {
- ProcessSerialNumber psn;
-
- err = GetCurrentProcess(&psn);
- if ( err != noErr) break;
-
- err =AECreateDesc(typeProcessSerialNumber, &psn, sizeof(ProcessSerialNumber), &theAddress);
- if ( err != noErr) break;
-
- dummyReply.descriptorType = typeNull;
- dummyReply.dataHandle = NULL;
-
- err = AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, &theAddress, kAutoGenerateReturnID, kAnyTransactionID, &theEvent);
- if ( err != noErr) break;
-
- err = AEPutParamDesc(&theEvent, keyDirectObject, &list);
- if ( err != noErr) break;
-
- err = AESend(&theEvent, &dummyReply, kAEWaitReply, kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
- if ( err != noErr) break;
-
-
- } while (false);
-
- return err;
- }
-
-
-
- OSStatus OpenFileDialog(OSType applicationSignature, short numTypes, OSType typeList[], NavEventProcPtr eventProc, FSSpec* fileSpec, OSType* fileType)
- {
- NavReplyRecord theReply;
- NavDialogOptions dialogOptions;
- OSErr theErr = noErr;
- NavTypeListHandle openList = NULL;
- NavEventUPP eventUPP = NewNavEventProc(eventProc);
-
- NavGetDefaultDialogOptions(&dialogOptions);
- dialogOptions.dialogOptionFlags -= kNavDontAddTranslateItems;
-
- BlockMoveData(LMGetCurApName(), dialogOptions.clientName, LMGetCurApName()[0] + 1);
-
- openList = (NavTypeListHandle)NewOpenHandle(applicationSignature, numTypes, typeList);
- if ( openList )
- {
- HLock((Handle)openList);
- }
-
- theErr = NavGetFile(NULL, &theReply, &dialogOptions, eventUPP, NULL, NULL, openList, NULL);
- DisposeRoutineDescriptor(eventUPP);
-
- if ( theErr != noErr && theErr != userCanceledErr )
- {
- ConductErrorDialog( theErr, 0, cancel );
- if (theErr == memFullErr)
- theErr = userCanceledErr;
- }
-
- if (theErr == noErr && theReply.validRecord)
- {
- // grab the target FSSpec from the AEDesc for opening:
- AEDesc resultDesc;
- FInfo fileInfo;
-
- // Is this a single file open
- if ( fileSpec != NULL )
- {
- AEKeyword keyword;
-
- theErr = AEGetNthDesc(&theReply.selection, 1, typeFSS, &keyword, &resultDesc);
- if (theErr == noErr)
- BlockMove(*resultDesc.dataHandle,fileSpec,sizeof(FSSpec));
-
- // decide if the doc we fileSpec opening is a PICT or TEXT
- theErr = FSpGetFInfo(fileSpec, &fileInfo);
- if (theErr == noErr)
- {
- if ( fileType != NULL )
- *fileType = fileInfo.fdType;
- }
- }
- else
- {
- // Multiple files open: use ApleEvents
- theErr = SendOpenAE(theReply.selection);
- }
-
- NavDisposeReply(&theReply);
- }
-
- if (openList != NULL)
- {
- HUnlock((Handle)openList);
- DisposeHandle((Handle)openList);
- }
-
- return theErr;
- }
-
-
- short ConfirmSaveDialog(StringPtr documentName, Boolean quitting, NavEventProcPtr eventProc)
- {
- OSStatus theStatusErr = noErr;
- OSErr theErr = noErr;
- NavAskSaveChangesResult reply = 0;
- NavAskSaveChangesAction action = 0;
- NavEventUPP eventUPP = NewNavEventProc(eventProc);
- NavDialogOptions dialogOptions;
- short result;
-
- if (quitting)
- action = kNavSaveChangesQuittingApplication;
- else
- action = kNavSaveChangesClosingDocument;
-
- BlockMoveData(LMGetCurApName(),dialogOptions.clientName,LMGetCurApName()[0]+1);
- BlockMoveData(documentName,dialogOptions.savedFileName,documentName[0]+1);
-
- theErr = NavAskSaveChanges( &dialogOptions,
- action,
- &reply,
- eventUPP,
- NULL);
- DisposeRoutineDescriptor(eventUPP);
-
- // Map reply code to ok, cancel, dontSave
- switch (reply)
- {
- case kNavAskSaveChangesSave:
- result = ok;
- break;
-
- case kNavAskSaveChangesCancel:
- result = cancel;
- break;
-
- case kNavAskSaveChangesDontSave:
- result = dontSaveChanges;
- break;
- }
-
- return result;
- }
-
-
-
- OSStatus SaveFileDialog(StringPtr fileName, OSType filetype, OSType fileCreator,
- NavEventProcPtr eventProc, FSSpec* fileSpec,
- Boolean* stationery, Boolean* replacing, NavReplyRecord* reply)
- {
- NavDialogOptions dialogOptions;
- OSErr theErr = noErr;
- NavEventUPP eventUPP = NewNavEventProc(eventProc);
-
- NavGetDefaultDialogOptions(&dialogOptions);
-
- dialogOptions.dialogOptionFlags |= (stationery != NULL ? kNavAllowStationery : 0);
- BlockMoveData(fileName, dialogOptions.savedFileName, fileName[0] + 1);
- BlockMoveData(LMGetCurApName(), dialogOptions.clientName, LMGetCurApName()[0] + 1);
-
- theErr = NavPutFile(NULL, reply, &dialogOptions, eventUPP, filetype, fileCreator, NULL);
- DisposeRoutineDescriptor(eventUPP);
-
- if (reply->validRecord)
- {
- // User saved
- AEDesc resultDesc;
- AEKeyword keyword;
-
- // retrieve the returned selection:
- theErr = AEGetNthDesc(&reply->selection, 1, typeFSS, &keyword, &resultDesc);
- if (theErr == noErr)
- BlockMove(*resultDesc.dataHandle, fileSpec, sizeof(FSSpec));
-
- if ( replacing != NULL )
- *replacing = reply->replacing;
-
- if ( stationery != NULL )
- {
- *stationery = reply->isStationery;
- }
- }
- else
- {
- // User cancelled
- if ( replacing != NULL )
- *replacing = false;
-
- if ( stationery != NULL )
- *stationery = false;
-
- theErr = userCanceledErr;
- }
-
- return theErr;
- }
-
-
-
- OSStatus CompleteSave(const FSSpec*, NavReplyRecord* reply)
- {
- OSStatus theErr;
-
- if (reply->validRecord)
- {
- theErr = NavCompleteSave(reply, kNavTranslateInPlace);
- }
-
- theErr = NavDisposeReply(reply);
-
- return theErr;
- }
-
-
-
-
-
-
-
- //
- // Callback to handle events that occur while navigation dialogs are up but really should be handled by the application
- //
-
- extern void HandleEvent(EventRecord * pEvent);
-
-
- pascal void MyEventProc(const NavEventCallbackMessage callBackSelector,
- NavCBRecPtr callBackParms,
- NavCallBackUserData /*callBackUD*/)
- // Callback to handle event passing betwwn the navigation dialogs and the applicatio
- {
- if ( callBackSelector == kNavCBEvent )
- switch (callBackParms->eventData.event->what)
- {
- case updateEvt:
- HandleEvent(callBackParms->eventData.event);
- break;
- }
- }
-
-